package com.example.oauth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    Logger logger = LoggerFactory.getLogger(MyUsernamePasswordAuthenticationFilter.class);

//    RequestCache requestCache = new HttpSessionRequestCache();

    public MyUsernamePasswordAuthenticationFilter() {
        super("/authentication");
    }

    @Override
    @Autowired
    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        super.setAuthenticationManager(authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
        String reqUri = httpServletRequest.getRequestURI();

        logger.debug("attempt MyUsername password authentication: {}", reqUri);
        String username = httpServletRequest.getParameter("username");
        String clientId = httpServletRequest.getParameter("clientId");
        String password = httpServletRequest.getParameter("password");
        MyAuthenticationToken token = new MyAuthenticationToken(new MyLoginPrincipal(username, clientId),
                clientId,
                password, null);
        Authentication authentication = this.getAuthenticationManager().authenticate(token);
        return authentication;
    }

//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
////        HttpServletRequest request = (HttpServletRequest) servletRequest;
////        HttpServletResponse response = (HttpServletResponse) servletResponse;
////        SavedRequest savedRequest = requestCache.getRequest(request, response);
////        String redirectUrl = savedRequest.getRedirectUrl();
////        logger.debug("#my filter: {}, \n redirectUrl: {}", request.getRequestURI(), redirectUrl);
//        filterChain.doFilter(servletRequest, servletResponse);
//    }
}
